<?php

class Db_Tables2_BClient
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetClientArr($keyword)
	{
		$result = array();
		
		$keyword = trim($keyword);
	
		if(1 < mb_strlen($keyword))
		{
			$select = $this->dealers_db->select();
			$select->from("b_client", array("client_id", "client_name"));
			$select->where("client_name like \"%".$keyword."%\" or cellphone like \"%".$keyword."%\" or address like \"%".$keyword."%\" or tel like \"%".$keyword."%\"");
			$select->where("status = ?", 2);
			
			if("LEVEL-1" == $_SESSION['identity']['level'])
			{
				$select->where("emp_id = ?", $_SESSION['identity']['userid']);
			}
			
			$data = $this->dealers_db->fetchAll($select);
			
			if(!empty($data))
			{
				foreach($data as $d)
				{
					$result[] = array(
						"id" => $d['client_id'],
						"name" => $d['client_name']
					);
				}
			}
		}
		
		return $result;
	}
	
	function FetchClientDetail($client_id, $full_info = NULL)
	{
		$result = array();
        
        $mod_params = new Db_Tables2_BDictParams();
        $sys_id = $mod_params->GetSysId();

		if($client_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_client", "*");
			$select->where("client_id = ?", $client_id);
			$row = $this->dealers_db->fetchRow($select);

			if($row)
			{
				$result = $row;
			}
			
			if($full_info)
			{
				$result['additional_info'] = array(
					"brand_info" => array(
						"arr" => array(),
						"str" => ""
					),
					"channel_info" => array(
						"arr" => array(),
						"str" => ""
					),
					"factory_info" => array(
						"arr" => array(),
						"str" => ""
					),
				);
			
				if($result['is_dealer'])
				{
					//相关品牌
					$select = $this->dealers_db->select();
					$select->from("dealers_db".$sys_id.".t_dealer_brand as d", array());
					$select->joinLeft("cars_db".$sys_id.".t_brand as b", "b.id=d.brandId", array("id", "name"));
					$select->where("d.dealerId = ?", $result['is_dealer']);
					$rows = $this->dealers_db->fetchAll($select);

					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['additional_info']['brand_info']['arr'][$row['id']] = $row['name'];
						}
						
						$result['additional_info']['brand_info']['str'] = implode(", ", $result['additional_info']['brand_info']['arr']);
					}
					
					//相关渠道
					$select = $this->dealers_db->select();
					$select->from("dealers_db".$sys_id.".t_dealer_channel as d", array());
					$select->joinLeft("hxb_db".$sys_id.".t_channel as b", "b.id=d.channelId", array("id", "name"));
					$select->where("d.dealerId = ?", $result['is_dealer']);
					$rows = $this->dealers_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['additional_info']['channel_info']['arr'][$row['id']] = $row['name'];
						}
						
						$result['additional_info']['channel_info']['str'] = implode(", ", $result['additional_info']['channel_info']['arr']);
					}
					
					//相关厂商
					$select = $this->dealers_db->select();
					$select->from("dealers_db".$sys_id.".t_dealer_factory as d", array());
					$select->joinLeft("cars_db".$sys_id.".t_factory as b", "b.id=d.factoryId", array("id", "name"));
					$select->where("d.dealerId = ?", $result['is_dealer']);
					$rows = $this->dealers_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['additional_info']['factory_info']['arr'][$row['id']] = $row['name'];
						}
						
						$result['additional_info']['factory_info']['str'] = implode(", ", $result['additional_info']['factory_info']['arr']);
					}
				}else{
					//相关厂商
					$select = $this->dealers_db->select();
					$select->from("dealers_db".$sys_id.".b_client_factory as d", array());
					$select->joinLeft("cars_db".$sys_id.".t_factory as b", "b.id=d.factory_id", array("id", "name"));
					$select->where("d.client_id = ?", $result['client_id']);
					$rows = $this->dealers_db->fetchAll($select);
					
					if(!empty($rows))
					{
						foreach($rows as $row)
						{
							$result['additional_info']['factory_info']['arr'][$row['id']] = $row['name'];
						}
						
						$result['additional_info']['factory_info']['str'] = implode(", ", $result['additional_info']['factory_info']['arr']);
					}
				}
				
			}
		}
		
		return $result;
	}
}